package net.cmkj.mine.handler;

import com.jfinal.handler.Handler;
import com.jfinal.log.Log;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局Handler，设置一些通用功能
 * 描述：主要是一些全局变量的设置，再就是日志记录开始和结束操作
 */
public class GlobalHandler extends Handler {
	
	private static final Log log = Log.getLog(GlobalHandler.class);

	@Override
	public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
		//String ctx = request.getContextPath();
		//request.setAttribute("ctx", ctx);//设置全局上下文
		//网络协议
		String protocol = request.getScheme();
		//网络ip
		String ip = request.getServerName();
		//端口号
		int port = request.getServerPort();
		//项目发布名称
		String webApp = request.getContextPath();
		//项目路径
		String ctx = protocol + "://" + ip + ":" + port + webApp;
		request.setAttribute("ctx", ctx);//设置全局上下文
//		log.debug("设置Header");
//		request.setAttribute("decorator", "none");
//		response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
//		response.setHeader("Pragma","no-cache"); //HTTP 1.0
//		response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
		next.handle(target, request, response, isHandled);
		log.debug("请求处理完毕，计算耗时");
	}
	
}
